home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / mutt / me2s_pl7.zoo / mu_edit2 / util / spoof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  2.8 KB  |  123 lines

  1. static char rcsid[] = "$Id: spoof.c,v 1.1 1992/09/06 19:31:32 mike Exp $";
  2.  
  3. /* $Log: spoof.c,v $
  4.  * Revision 1.1  1992/09/06  19:31:32  mike
  5.  * Initial revision
  6.  *
  7.  */
  8.  
  9. /* 
  10.  * spoof.c : kinda like sprintf
  11.  *    Craig Durland 8/87 modified from spewf    Public Domain
  12.  *    3/92 added stdarg support
  13.  */
  14.  
  15. #ifdef __STDC__
  16.  
  17. #include <stdarg.h>
  18. #define VA_START va_start
  19.  
  20. #else    /* __STDC__ */
  21.  
  22. #include <varargs.h>
  23. #define VA_START(a,b) va_start(a)
  24.  
  25. #endif
  26.  
  27. #include "const.h"
  28.  
  29. #define BLANKS \
  30.  "                                                                                "
  31. /*12345678901234567890123456789012345678901234567890123456789012345678901234567890*/
  32.  
  33. static void fillit(buf,string,width,left_just)
  34.   char *buf, *string; int width, left_just;
  35. {
  36.   int len = strlen(string);
  37.  
  38.   if (width == 0 || width > 80 || len == width) strcat(buf,string);
  39.   else
  40.     if (width < len) strncat(buf,string,width);
  41.     else
  42.       if (left_just) { strcat(buf,string); strncat(buf,BLANKS,width-len); }
  43.       else { strncat(buf,BLANKS,width-len); strcat(buf,string); }
  44. }
  45.  
  46. #ifdef __STDC__
  47. char *spoof(char *buf, char *format, ...)
  48. #else
  49. char *spoof(buf,format,va_alist) char *buf, *format; va_dcl
  50. #endif
  51. {
  52.   extern char *i_to_a(), *l_to_a(), *tobase();
  53.  
  54.   char c[2];
  55.   int left_just, width;
  56.   long x;
  57.  
  58.   va_list varptr;
  59.  
  60.   VA_START(varptr, format);
  61.  
  62.   *buf = c[1] = '\0';    /* for %c */
  63.   while (*format)
  64.   {
  65.     switch (*format)
  66.     {
  67.       case '%': left_just = FALSE; width = 0;
  68.     more:    /* process some more of '%' */
  69.         switch (*++format)
  70.         {
  71.       case '%': strcat(buf,"%"); break;
  72.       case '-': left_just = TRUE; goto more;
  73.       case 'c': c[0]=va_arg(varptr,int);
  74.         fillit(buf,c,width,left_just); break;
  75.       case 'd': fillit(buf,i_to_a(va_arg(varptr,int)),width,left_just);
  76.         break;
  77.       case 'l':    /* 'd' only */
  78.         fillit(buf,l_to_a(va_arg(varptr,long)),width,left_just);
  79.         format++; break;
  80.       case 's': fillit(buf,va_arg(varptr,char *),width,left_just);
  81.          break;
  82.       case 'u':
  83.         x = (long)va_arg(varptr,unsigned int);
  84.         fillit(buf,l_to_a(x),width,left_just);
  85.         break;
  86.       case 'x':
  87.         fillit(buf,tobase((long)va_arg(varptr,unsigned int),16),
  88.             width,left_just);
  89.         break;
  90.       case '0': case '1': case '2': case '3': case '4': case '5':
  91.       case '6': case '7': case '8': case '9':
  92.         width = width*10 +*format -'0'; goto more;
  93.       case '\0': format--; break;    /* format not finished => ignore */
  94.       default:     /* copy %<char not matched> */
  95.         strcat(buf,"%"); *c = *(format-1); strcat(buf,c);
  96.         }
  97.     break;
  98.       default: *c = *format; strcat(buf,c);
  99.     }
  100.     format++;
  101.   }
  102.  
  103.   va_end(varptr);
  104.   return buf;
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. /* ****************  TEST ********************* */
  113. #ifdef TEST
  114.  
  115. main()
  116. {
  117.   char buf[200];
  118.  
  119.   puts(spoof(buf, "This is a %s %d", "test", 123));
  120. }
  121.  
  122. #endif
  123.